package com.wbc.demo.CompletableFutureDemo;

import java.util.Map;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BiFunction;


/**
 * 异步处理等待最后一个完成
 * 一个接口返回结果需要几个方法同时完成之后才可以继续，并且这几个方法执行没有先后顺序，可以使用本Demo
 */
public class CompletableFutureDemo2 {


    public static void main(String[] args) throws ExecutionException, InterruptedException {

        //创建一个自定义的线程池
        ExecutorService executorService = Executors.newFixedThreadPool(5);

        AtomicReference<String> str = new AtomicReference();
        System.out.println("开始一部");

        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {
            System.out.println("开始启动1");
            for (int i = 0; i < 10; i++) {
                System.out.println("开始启动1:" + i);
                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.println("运行完成1");
        });

        CompletableFuture<Void> voidCompletableFuture1 = CompletableFuture.runAsync(() -> {
            System.out.println("开始启动");
            for (int i = 0; i < 10; i++) {
                System.out.println(i);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.println("运行完成");
        });
        CompletableFuture<Object> objectCompletableFuture = CompletableFuture.anyOf(
                voidCompletableFuture,
                voidCompletableFuture1
        );
        Object join = objectCompletableFuture.join();
        System.out.println(join);
        System.out.println("继续后面的操作");
        Thread.sleep(10000);



    }




}

